/**
 * Module dependencies.
 */

var express = require('express'),
  routes = require('./routes');
var MongoStore = require('connect-mongo')(express);
var dbcfg = require('./dbcfg');

var app = module.exports = express.createServer();

// Configuration



app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    secret: dbcfg.cookieSecret,
    store: new MongoStore({
      db: dbcfg.db
    })
  }));
  app.use(express.router(routes))
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function() {
  app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
  }));
});

app.configure('production', function() {
  app.use(express.errorHandler());
});

app.dynamicHelpers({
  user: function(req, res) {
    return req.session.user;
  },
  error:function(req,res){
    var error = req.flash('error');
    if(error.length)
      return error;
    else
      return null;
  },
  success:function(req,res){
    var succ = req.flash('success');
    if(succ.length)
      return succ;
    else
      return null;
  },
})



// Routes

// app.get('/', routes.index);
// app.get('/u/:user', routes.user);
// app.post('/post', routes.post);
// app.get('/reg', routes.reg);
// app.post('/reg', routes.doReg);
// app.get('/login', routes.login);
// app.post('/login', routes.doLogin);
// app.get('/logout');

// app.get('/user/:username', function(req, res) {
//   res.send('user: ' + req.params.username);
// });
// app.get ('/list',function(req,res){
//   res.render('list',{
//     title:'list',
//     items:[1991,'lyh','express','Node.js','asasdasd']
//   });
// });


app.listen(3000, function() {
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});